<template>
  <el-dialog
    class="main_reset_pwd_dialog"
    width="1000"
    :modelValue="modelValue"
    draggable
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
    align-center
  >
    <div class="flex h-[460px] items-center">
      <div
        class="w-[380px] bg-[var(--base-pwd-bg)] h-full flex items-center justify-center"
      >
        <div>
          <img src="./imgs/pwd.png" alt="" class="block mx-auto" />
          <div
            class="text-center font-bold mb-[6px] text-[var(--base-text-color)]"
          >
            系统账号安全管控
          </div>
          <div class="text-center text-[red]">
            {{ pwd_overdue_tip }}
          </div>
        </div>
      </div>
      <div class="w-[620px] px-[64px]">
        <template v-if="isShow == 1">
          <div
            class="base"
            :class="user.verify_type == 1 ? 'active' : ''"
            @click="handleAdd(1)"
            @mouseover="isHovered = true"
            @mouseout="isHovered = false"
          >
            <div class="flex items-center">
              <SvgIcon
                icon-class="SuccessFilled"
                v-if="user.verify_type == 1"
              />
              <span class="ml-[8px]">旧密码验证</span>
            </div>
            <div class="relative">
              <SvgIcon
                icon-class="suo1"
                className="icon_base text-[#eee]"
                v-if="!isHovered"
              />
              <SvgIcon
                icon-class="suo"
                class="icon_base text-[#EAF3FF]"
                v-if="isHovered"
              />
              <SvgIcon
                icon-class="xingxing"
                class="text-[72px] icon_base text-[#EAF3FF] right-[-8px]"
                v-if="isHovered"
              />
              <!-- <img src="./imgs/suo.png" alt="" /> -->
            </div>
          </div>
          <div
            class="base mt-[50px]"
            :class="user.verify_type == 2 ? 'active' : ''"
            @click="handleAdd(2)"
            @mouseover="isHovered1 = true"
            @mouseout="isHovered1 = false"
          >
            <div class="flex items-center">
              <SvgIcon
                icon-class="SuccessFilled"
                v-if="user.verify_type == 2"
              />
              <span class="ml-[8px]">飞书扫码验证</span>
            </div>
            <div class="relative">
              <SvgIcon
                icon-class="yanzheng"
                class="icon_base text-[#eee]"
                v-if="!isHovered1"
              />
              <SvgIcon
                icon-class="yanzheng1"
                class="icon_base text-[#EAF3FF]"
                v-if="isHovered1"
              />
              <SvgIcon
                icon-class="xingxing"
                class="text-[72px] icon_base text-[#EAF3FF] right-[-8px]"
                v-if="isHovered1"
              />
              <!-- <img src="./imgs/yanzheng.png" alt="" /> -->
            </div>
          </div>
        </template>
        <template v-if="isShow == 2">
          <el-form
            ref="formRef"
            :model="user"
            :rules="rules"
            label-position="top"
          >
            <el-form-item
              label="旧密码"
              prop="old_password"
              v-if="user.verify_type == 1"
            >
              <el-input
                v-model="user.old_password"
                placeholder="请输入旧密码"
                type="password"
                show-password
                name="old_password"
              />
            </el-form-item>
            <el-form-item label="新密码" prop="new_password">
              <el-input
                v-model="user.new_password"
                placeholder="请输入新密码"
                type="password"
                show-password
                name="new_password"
              />
            </el-form-item>
            <el-form-item label="确认密码" prop="sure_password">
              <el-input
                v-model="user.sure_password"
                placeholder="请确认新密码"
                type="password"
                show-password
                name="sure_password"
              />
            </el-form-item>
          </el-form>
          <div class="text-center">
            <el-button
              round
              class="mr-[30px]"
              :disabled="loading"
              @click="handleLast"
              >上一步</el-button
            >
            <el-button
              v-if="!disabled"
              type="primary"
              :loading="loading"
              round
              @click="submit"
            >
              确认
            </el-button>
          </div>
        </template>
      </div>
    </div>
    <Authorization
      v-model="authorizationVisible"
      :authorizationData="authorizationData"
      @close="authorizationData = {}"
      @refresh="handleAuth"
    />
  </el-dialog>
</template>

<script setup>
import { updateUserForcePwd } from "@/api/system/user";
import useUserStore from "@/store/modules/user";
import Authorization from "./components/authorization/index.vue";
import SvgIcon from "@/components/SvgIcon/index.vue";
const props = defineProps({
  modelValue: { type: Boolean, default: false },
  isClose: { type: Boolean, default: true },
});
const disabled = ref(false);
const loading = ref(false);
const userStore = useUserStore();
const pwd_overdue_tip = computed(() => userStore.pwd_overdue_tip);
const { proxy } = getCurrentInstance();

const emit = defineEmits(["update:modelValue"]);

const isShow = ref(1);
const isHovered = ref(false);
const isHovered1 = ref(false);
const user = ref({
  old_password: null,
  new_password: null,
  sure_password: null,
  verify_type: null,
  fs_rt_qrcode: null,
});
// 密码规则正则
const passwordReg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{8,}$/;
// 表单校验规则
const rules = ref({
  old_password: [
    { required: true, message: "旧密码不能为空", trigger: "blur" },
  ],
  new_password: [
    { required: true, message: "新密码不能为空", trigger: "blur" },
    {
      min: 8,
      max: 20,
      message: "密码长度需在 8 到 20 个字符之间",
      trigger: "blur",
    },
    {
      validator: (rule, value, callback) => {
        if (!passwordReg.test(value)) {
          callback(new Error("密码需8位以上，包含英文大小写和数字"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
  sure_password: [
    { required: true, message: "确认密码不能为空", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        if (value !== user.value.new_password) {
          callback(new Error("两次输入的密码不一致"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
});
const formRef = ref();
const authorizationVisible = ref(false);
const authorizationData = ref({});
const generateUniqueAutocompleteId = () => {
  return `off_${Math.random().toString(36).substr(2, 10)}`;
};
/** 提交按钮 */
function submit() {
  formRef.value.validate((valid) => {
    if (valid) {
      if (user.value.verify_type == 2) {
        authorizationData.value = user.value;
        authorizationVisible.value = true;
        return;
      }
      const params = {
        ...user.value,
      };
      updateUserForcePwd(params).then((response) => {
        proxy.$modal.msgSuccess("修改成功");
        handleClose();
      });
    }
  });
}

const handleAdd = (val) => {
  user.value.verify_type = val;
  isShow.value = 2;
};

const handleLast = () => {
  isShow.value = 1;
  user.value = {
    old_password: null,
    new_password: null,
    sure_password: null,
    verify_type: null,
    fs_rt_qrcode: null,
  };
};
const handleAuth = () => {
  authorizationData.value = {};
  handleClose();
};
const handleClose = () => {
  isShow.value = 1;
  emit("update:modelValue", false);
  proxy.$refs.pwdRef?.resetFields();
  location.reload();
};
</script>
<style lang="scss">
.main_reset_pwd_dialog {
  .el-dialog__header {
    display: none;
  }
  .el-dialog__body {
    padding: 0 !important;
    border-radius: 12px;
  }
  .base {
    display: flex;
    width: 100%;
    height: 72px;
    justify-content: space-between;
    margin: 0 auto;
    border: 1px solid #dcdfe6;
    padding: 10px;
    align-items: center;
    cursor: pointer;
    border-radius: 5px;
    margin-bottom: 10px;
    transition: border-color 0.3s ease;
    &.active {
      background-color: var(--base-table-header-bg-color);
      border: 1px solid #5098ff;
    }
    &:hover {
      border-color: rgba(52, 135, 255, 0.5);
      box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    }
  }
}
.icon_base {
  font-size: 52px;
  position: absolute;
  top: -25px;
  right: 0;
}
</style>
